package cn.tedu.lianxi1;

import javax.lang.model.SourceVersion;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Text5 {
    public static void main(String[] args) throws Exception {
   /*     method();//获取class对象
        System.out.println("------");
        method2();//获取构造方法
        System.out.println("------");
        method3();//获取成员方法
        System.out.println("------");
        method4();//获取成员变量
        System.out.println("------");
        method5();//通过反射的技术创建对象

    */
     /*   A1 a=new A1();
        a.method();
        System.out.println("---------");
        a.method2();
        System.out.println("---------");
        a.method3();
        System.out.println("---------");
        a.method4();
        System.out.println("---------");
        a.method5();

      */
        A2 a1=new A2();
        a1.method();
        System.out.println("----------");
        a1.method2();
        System.out.println("----------");
        a1.method3();
        System.out.println("----------");
        a1.method4();
        System.out.println("----------");
        a1.method5();
    }

    public static void method5() throws Exception {
        Class<Tianshu> a=Tianshu.class;
        Tianshu a1= a.newInstance();
        System.out.println("a1 = " + a1);
        Constructor<Tianshu> a2=a.getConstructor(String.class);
        Tianshu a3=a2.newInstance("无名");
        System.out.println("a3 = " + a3);
        Constructor<Tianshu> c = a.getConstructor(String.class, int.class);
        Tianshu a4=c.newInstance("无名",300);
        System.out.println("a4 = " + a4);
        Constructor<Tianshu> c2 = a.getConstructor(String.class, int.class, double.class);
        Tianshu a5=c2.newInstance("雄霸",50,9999999);
        System.out.println("a5 = " + a5);
    }

    public static void method4() {
        Class a=Tianshu.class;
        Field[] f = a.getFields();
        for (Field f1 : f) {
            System.out.println(f1.getName());
            System.out.println(f1.getType().getName());
        }
    }

    public static void method3() {
        Class a=Tianshu.class;
        Method[] m = a.getMethods();
        for (Method m1 : m) {
            System.out.println(m1.getName());
            Class<?>[] p = m1.getParameterTypes();
            System.out.println(Arrays.toString(p));
        }
    }

    public static void method2() {
        Class a=Tianshu.class;
        Constructor[] c = a.getConstructors();
        for (Constructor c1 : c) {
            System.out.println(c1.getName());
            Class[] p = c1.getParameterTypes();
            System.out.println(Arrays.toString(p));
        }
    }

    public static void method() {
        Class a=Tianshu.class;
        System.out.println("a = " + a);
    }
}
class A1{
    public void method(){
        Class a=Tianshu.class;
        System.out.println("a = " + a);
    }
    public void method2(){
        Class a=Tianshu.class;
        Constructor[] c = a.getConstructors();
        for (Constructor c1 : c) {
            System.out.println(c1.getName());
            Class[] p = c1.getParameterTypes();
            System.out.println(Arrays.toString(p));
        }
    }
    public void method3(){
        Class a=Tianshu.class;
        Method[] m = a.getMethods();
        for (Method m1 : m) {
            System.out.println(m1.getName());
            Class<?>[] p = m1.getParameterTypes();
            System.out.println(Arrays.toString(p));
        }
    }
    public void method4(){
        Class a=Tianshu.class;
        Field[] f = a.getFields();
        for (Field f1 : f) {
            System.out.println(f1.getName());
            System.out.println(f1.getType().getName());
        }
    }
    public void method5() throws Exception {
        Class<Tianshu> a=Tianshu.class;
        Tianshu a1=a.newInstance();
        System.out.println("a1 = " + a1);
        Constructor<Tianshu> c = a.getConstructor(String.class);
        Tianshu a2=c.newInstance("无名");
        System.out.println("a2 = " + a2);
        Constructor<Tianshu> c1 = a.getConstructor(String.class, int.class);
        Tianshu a3 = c1.newInstance("梦", 22);
        System.out.println("a3 = " + a3);
        Constructor<Tianshu> c2 = a.getConstructor(String.class, int.class, double.class);
        Tianshu a4 = c2.newInstance("天道", 3000, 9999999.9999);
        System.out.println("a4 = " + a4);


    }
}
class A2{
    public void method(){
         Class a=Tianshu.class;
        System.out.println("a = " + a);
    }
    public void method2(){
        Class a=Tianshu.class;
        Constructor[] c = a.getConstructors();
        for (Constructor c1 : c) {
            System.out.println(c1.getName());
            Class[] p = c1.getParameterTypes();
            System.out.println(Arrays.toString(p));
        }
    }
    public void method3(){
        Class a=Tianshu.class;
        Method[] m = a.getMethods();
        for (Method m1 : m) {
            System.out.println(m1.getName());
            Class<?>[] p = m1.getParameterTypes();
            System.out.println(Arrays.toString(p));
        }
    }
    public void method4(){
        Class a=Tianshu.class;
        Field[] f = a.getFields();
        for (Field f1 : f) {
            System.out.println(f1.getName());
            System.out.println(f1.getType().getName());
        }
    }
    public void method5() throws Exception {
        Class<Tianshu> a=Tianshu.class;
        Tianshu a1 = a.newInstance();
        System.out.println("a1 = " + a1);
        Constructor<Tianshu> c = a.getConstructor(String.class);
        Tianshu a2 = c.newInstance("天下");
        System.out.println("a2 = " + a2);
        Constructor<Tianshu> c1 = a.getConstructor(String.class, int.class);
        Tianshu a3 = c1.newInstance("大佛", 500);
        System.out.println("a3 = " + a3);
        Constructor<Tianshu> c2 = a.getConstructor(String.class, int.class, double.class);
        Tianshu a4 = c2.newInstance("洞天福地", 600, 88888.666);
        System.out.println("a4 = " + a4);
    }
}
